﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace HYRElectric.Calculate
{
	internal class CalUtility
	{
		public CalUtility(UIApplication application)
		{
			this.m_uiApplication = application;
			this.m_appCreator = this.m_uiApplication.Application;
		}

		public static ROOMTYPE GetRoomTypeByString(string sFJLX)
		{
			ROOMTYPE result = ROOMTYPE.eRect;
			if (sFJLX.CompareTo("矩形") == 0)
			{
				result = ROOMTYPE.eRect;
			}
			else if (sFJLX.CompareTo("多边形") == 0)
			{
				result = ROOMTYPE.ePoly;
			}
			else if (sFJLX.CompareTo("弧形") == 0)
			{
				result = ROOMTYPE.eArc;
			}
			else if (sFJLX.CompareTo("弓形") == 0)
			{
				result = ROOMTYPE.eBow;
			}
			else if (sFJLX.CompareTo("扇形") == 0)
			{
				result = ROOMTYPE.eSector;
			}
			return result;
		}

		public static List<XYZ> GetRoomKeyPoint(UIApplication uiApp, Space room, string sFJLX)
		{
			List<XYZ> list = new List<XYZ>();
			if (room == null)
			{
				MessageBox.Show("空间对象为空，请检查");
				return null;
			}
			foreach (IList<BoundarySegment> list2 in room.GetBoundarySegments(new SpatialElementBoundaryOptions()))
			{
				foreach (BoundarySegment segment in list2)
				{
					IList<XYZ> list3 = segment.GetCurve().Tessellate();
					list.Add(list3[0]);
					list.Add(list3[list3.Count - 1]);
				}
			}
			return list;
		}

		public void CalRoomLayRowAndCol(IllCalData data)
		{
			DGVRowData activeDGVRowData = data.activeDGVRowData;
			LayoutData layoutData = activeDGVRowData.layoutData;
			CalResultData calResultData = activeDGVRowData.calResultData;
			ROOMTYPE roomTypeByString = CalUtility.GetRoomTypeByString(activeDGVRowData.sFJLX);
			switch (roomTypeByString)
			{
			case ROOMTYPE.eRect:
				this.CalRecLayRowAndCol(ref activeDGVRowData.arPtRoom, layoutData.nDJNum, layoutData.dBJB, activeDGVRowData.dAZGD, ref layoutData.nRowNum, ref layoutData.nColNum, ref calResultData.dAA, ref calResultData.dBB);
				return;
			case ROOMTYPE.ePoly:
				this.CalPolyLayRowAndCol(ref activeDGVRowData.arPtRoom, layoutData.nDJNum, layoutData.dBJB, activeDGVRowData.dAZGD, ref layoutData.nRowNum, ref layoutData.nColNum, ref calResultData.dAA, ref calResultData.dBB);
				return;
			case ROOMTYPE.eArc:
			case ROOMTYPE.eBow:
			case ROOMTYPE.eSector:
				this.CalArcLayCol(roomTypeByString, ref activeDGVRowData.arPtRoom, layoutData.nDJNum, activeDGVRowData.dAZGD, ref layoutData.nColNum, ref calResultData.dAA, ref calResultData.dBB);
				return;
			default:
				return;
			}
		}

		private void CalRecLayRowAndCol(ref List<XYZ> ptAry, int nSum, double dBDis, double dInstallH, ref int nRow, ref int nCol, ref double aa, ref double bb)
		{
			if (ptAry.Count < 4)
			{
				return;
			}
			if (nSum != 1)
			{
				double num = ptAry[0].DistanceTo(ptAry[3]) / ptAry[0].DistanceTo(ptAry[1]);
				double num2 = Math.Sqrt((double)nSum / num);
				double num3 = num2 - Math.Floor(num2);
				nCol = ((num3 > 0.5) ? ((int)Math.Ceiling(num2)) : ((int)Math.Floor(num2)));
				if (nCol > 0)
				{
					num3 = (double)nSum / (double)nCol - Math.Floor((double)nSum / (double)nCol);
					nRow = ((num3 > 0.5) ? ((int)Math.Ceiling((double)nSum / (double)nCol)) : ((int)Math.Floor((double)nSum / (double)nCol)));
				}
				else
				{
					nRow = 0;
				}
			}
			else
			{
				nCol = 1;
				nRow = 1;
			}
			if (nRow <= 0 || nCol <= 0 || dInstallH <= 0.0)
			{
				aa = 0.0;
				bb = 0.0;
				return;
			}
			double num4 = 2.0 * dBDis + (double)nRow - 1.0;
			if (num4 > 0.0)
			{
				aa = AssistFunc.feetToMM(ptAry[0].DistanceTo(ptAry[3])) / num4 / 1000.0 / dInstallH;
			}
			else
			{
				aa = 0.0;
			}
			num4 = 2.0 * dBDis + (double)nCol - 1.0;
			if (num4 > 0.0)
			{
				bb = AssistFunc.feetToMM(ptAry[0].DistanceTo(ptAry[1])) / num4 / 1000.0 / dInstallH;
				return;
			}
			bb = 0.0;
		}

		private void CalPolyLayRowAndCol(ref List<XYZ> ptAry, int nSum, double dBDis, double dInstallH, ref int nRow, ref int nCol, ref double aa, ref double bb)
		{
			XYZ xyz = new XYZ();
			XYZ xyz2 = new XYZ();
			GeoUtil.GetPolyBoundPts(ptAry, ref xyz, ref xyz2);
			XYZ xyz3 = new XYZ(xyz.X, xyz2.Y, 0.0);
			double num = xyz3.DistanceTo(xyz2) / xyz3.DistanceTo(xyz);
			double num2 = Math.Sqrt((double)nSum / num);
			double num3 = num2 - Math.Floor(num2);
			nRow = ((num3 > 0.5) ? ((int)Math.Ceiling(num2)) : ((int)Math.Floor(num2)));
			num3 = (double)nSum / (double)nRow - Math.Floor((double)nSum / (double)nRow);
			nCol = ((num3 > 0.5) ? ((int)Math.Ceiling((double)nSum / (double)nRow)) : ((int)Math.Floor((double)nSum / (double)nRow)));
			aa = AssistFunc.feetToMM(xyz3.DistanceTo(xyz2)) / (2.0 * dBDis + (double)nRow - 1.0) / 1000.0 / dInstallH;
			bb = AssistFunc.feetToMM(xyz3.DistanceTo(xyz)) / (2.0 * dBDis + (double)nCol - 1.0) / 1000.0 / dInstallH;
		}

		private void CalArcLayCol(ROOMTYPE eRoomtype, ref List<XYZ> ptAry, int nSum, double dInstallH, ref int nCol, ref double aa, ref double bb)
		{
			aa = 0.0;
			bb = 0.0;
			double num = 0.5;
			double num2 = 0.0;
			double num3 = 0.0;
			double value = 0.0;
			double num4 = 0.0;
			double num5 = 0.0;
			double num6 = 0.0;
			switch (eRoomtype)
			{
			case ROOMTYPE.eArc:
				if (ptAry.Count != 6)
				{
					MessageBox.Show("空间关键点获取错误！");
					return;
				}
				try
				{
					Arc arc = this.m_appCreator.CreatYJKArc(ptAry[0], ptAry[1], ptAry[2]);
					num4 = arc.Length;
					Arc arc2 = this.m_appCreator.CreatYJKArc(ptAry[3], ptAry[4], ptAry[5]);
					num5 = arc2.Length;
					XYZ xyz = ptAry[0] + (ptAry[2] - ptAry[0]) / 2.0;
					XYZ xyz2 = ptAry[3] + (ptAry[5] - ptAry[3]) / 2.0;
					XYZ arcMidPoint = ArcAssistFunc.GetArcMidPoint(arc);
					XYZ arcMidPoint2 = ArcAssistFunc.GetArcMidPoint(arc2);
					num6 = arcMidPoint.DistanceTo(arcMidPoint2);
					num3 = arc.Radius;
					value = arc.GetYJKEndParameter(1) - arc.GetYJKEndParameter(0);
					num2 = xyz.DistanceTo(xyz2);
					goto IL_30A;
				}
				catch (Exception)
				{
					MessageBox.Show("未能正确获取辅助圆弧，请检查空间类型设置是否正确");
					goto IL_30A;
				}
				break;
			case ROOMTYPE.eBow:
				break;
			case ROOMTYPE.eSector:
				goto IL_271;
			default:
				goto IL_30A;
			}
			if (ptAry.Count != 4)
			{
				MessageBox.Show("空间关键点获取错误！");
				return;
			}
			try
			{
				Arc arc3 = this.m_appCreator.CreatYJKArc(ptAry[0], ptAry[1], ptAry[2]);
				num4 = arc3.Length;
				num5 = 0.0;
				XYZ xyz3 = ptAry[0] + (ptAry[2] - ptAry[0]) / 2.0;
				XYZ arcMidPoint3 = ArcAssistFunc.GetArcMidPoint(arc3);
				num6 = xyz3.DistanceTo(arcMidPoint3);
				num2 = xyz3.DistanceTo(ptAry[0]);
				num3 = arc3.Radius;
				value = arc3.GetYJKEndParameter(1) - arc3.GetYJKEndParameter(0);
				goto IL_30A;
			}
			catch (Exception)
			{
				MessageBox.Show("未能正确获取辅助圆弧，请检查空间类型设置是否正确");
				goto IL_30A;
			}
			IL_271:
			if (ptAry.Count != 4)
			{
				MessageBox.Show("空间关键点获取错误！");
				return;
			}
			try
			{
				Arc arc4 = this.m_appCreator.CreatYJKArc(ptAry[0], ptAry[1], ptAry[2]);
				num4 = arc4.Length;
				num5 = 0.0;
				num6 = ptAry[0].DistanceTo(ptAry[3]);
				num2 = arc4.Radius;
				num3 = arc4.Radius;
				value = arc4.GetYJKEndParameter(1) - arc4.GetYJKEndParameter(0);
			}
			catch (Exception)
			{
				MessageBox.Show("未能正确获取辅助圆弧，请检查空间类型设置是否正确");
			}
			IL_30A:
			num2 = AssistFunc.feetToMM(num2);
			num3 = AssistFunc.feetToMM(num3);
			num4 = AssistFunc.feetToMM(num4);
			num5 = AssistFunc.feetToMM(num5);
			num6 = AssistFunc.feetToMM(num6);
			double num7 = num4 / num6;
			double num8 = num5 / num6;
			double num9 = Math.Sqrt((double)(2 * nSum) / (num7 + num8));
			nCol = (int)Math.Floor(num9);
			int num10 = (int)Math.Ceiling(num7 * num9);
			double num11 = num2 / (2.0 * num + (double)nCol - 1.0);
			if (dInstallH > 0.0)
			{
				if (2.0 * num + (double)num10 - 1.0 > 0.0)
				{
					aa = (num3 - num11 * num) * Math.Abs(value) / (2.0 * num + (double)num10 - 1.0) / 1000.0 / dInstallH;
				}
				bb = num11 / 1000.0 / dInstallH;
			}
		}

		protected UIApplication m_uiApplication;

		protected Autodesk.Revit.ApplicationServices.Application m_appCreator;
	}
}
